<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Cart extends MY_Controller {

     function __construct() {
     	
        parent::__construct();
        
        if(!$this->checkUserLogin()){
        	$user = $this->session->userdata('customer');
        	redirect('auth/login');
        	return;
        }
        
        $this->load->helper('url');
        $this->load->library('parser');
        //$this->load->library('cart');
        //$this->template->set_template('home');
        $this->template->add_region('new_tours');
        $this->load->model('cart_model', 'cart', true);
        $this->load->model('product_model', 'product', true);
        //$this->load->model('popup_model', 'popup', true);
        //$this->load->model('ncategory_model', 'ncategory', true);
        //$this->buildFrontEndTopMenu();
        //$this->buildSlider();
        
        $this->load->helper('form');
        $this->buildNavigation();
        $this->buildSearchByConditionBox();
        $this->buildSupportBox();
		//$this->buildAdvBox();
		//$this->buildSlider();
		//$this->buildCartBox();
		//$this->buildCompareBox();

     }
    
	function index() {
	
        $data['base_url'] = base_url();
        
        $this->view();
        
        //$this->template->parse_view('center_content1', 'default/front_end/home/new_product_view', $data);
        //$this->buildLeftContent();

        //$this->template->render();  
    }
	
    function view(){
    	$member_id = $this->session->userdata('customer'); //$this->session->userdata('member_id');
        $member_id = $member_id['id_user'];
    	$data['lb_name'] = lang('name');
    	$data['lb_picture'] = lang('picture');
    	$data['lb_quantity'] = lang('quantity');
    	$data['lb_price'] = lang('price');
    	$data['lb_total'] = lang('total');
    	$data['lb_total_amount'] = lang('total_amount');
    	$data['shopping_cart'] = lang('shopping_cart');
    	$data['home'] = lang('home');
        $data['cart'] = $this->cart->getCartMember( $member_id);
        
       // if (empty($data['products']))
       // 	echo "Shopping cart is empty.";
        
        //print_r($data['products']);
        
        $lang = getLangCode();
        
        $totalAmount = 0;
        
        	foreach ($data['cart'] as &$item){
        		$item['name'] = $item[ $lang . '_name'];
        		$item['img'] = img(array('src' => getDirByDate($item['date_add'], PRODUCT_IMAGE_PATH) . '/' . $item['image'],  'height' => '60', 'width' => '60' ));
        		$item['delete'] = anchor('cart/delete/' . $item['product_id'], 'Delete', 'title="Delete '. $item['name'] .'"');
        		$item['total'] = $item['price'] * $item['quantity'] ;
        		$item['quantity'] = form_input(array(
             					 'name'        => 'quantity[]',
              					//'id'          => 'price',
              					'value'       => $item['quantity'],
              					'maxlength'   => '100',
              					'size'        => '50',
              					'style'       => 'width:50px',
           						 ));
           		$item['product_input'] = form_hidden( 'product_id[]' , $item['product_id']);
           		$totalAmount += $item['total'];
        	}
        $data['update_shopping_cart'] = anchor('#', lang('update_shopping_cart'), ' class="buttonlink" id="update_cart"');
        $data['process_to_checkout'] = anchor('checkout', lang('process_to_checkout'), ' class="buttonlink"');
        $data['continue_shopping'] = anchor('product', lang('continue_shopping'), ' class="buttonlink" title="Continue Shopping"');
        
        $data['action'] = site_url('cart/update');
        $data['total_amount'] = $totalAmount;
        
        $this->template->parse_view('top_content1', 'default/front_end/cart/cart_view',$data);
        $this->template->render();  
    }
    
	function add($product_id) {
		
    	$user = $this->session->userdata('customer');
    	$member_id = $user['id_user'];
//    	if (empty($member_id)){
//    		redirect('login');
//    		return;
//    	}

    	$product = $this->product->getById($product_id);
    	
    	if (!empty($product)){
    		$item = $this->cart->getCartMember($member_id, $product_id);
    		if (empty($item))
    			$result = $this->cart->insert(array('member_id' => $member_id, 'product_id' => $product_id, 'quantity' => 1));
    		else 
    			$result = $this->cart->update(array('member_id' => $member_id, 'product_id' => $product_id, 'quantity' => 1 + $item[0]['quantity']));
    	}
    	
    	$this->view();
    }
    
	function update() {
		
    	$member_id = $this->session->userdata('member_id');
    	
    	if (empty($member_id)){
    		redirect('login');
    		return;
    	}
    	
    	$product_ids = $this->input->post('product_id');
    	$product_quantities = $this->input->post('quantity');
    	
    	//print_r($product_ids);
    	//print_r($_POST);
    	//echo "Number item: " . count($product_ids);
    	//return ;
    	
    	for ($i = 0 ; $i < count($product_ids); $i++){
    		$product_id = $product_ids[$i];
    		$quantity = intval($product_quantities[$i]);
    		if ($quantity == 0){
    			$this->cart->delete($member_id, $product_id);
    		}
    		else{
    			$this->cart->update(array('member_id' => $member_id, 'product_id' => $product_id, 'quantity' => $quantity));
    		}
    	}
    	
    	$this->view();
    }

    function buildLeftContent() {
        $data['menuitems'] = array(
            array(
                'status' => 'active',
                'click_function' => 'get_list(current_from, this);',
                'text' => 'Add New Child',
                'href' => site_url('children/add_child'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'create_user(this);',
                'text' => 'View All Children ',
                'href' => site_url('children/view_all'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'system_logs(this);',
                'text' => 'View Current Children ',
                'href' => site_url('children/view_current'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'login_logs(this);',
                'text' => 'View Graduated Children ',
                'href' => site_url('children/view_graduated'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'system_config(this);',
                'text' => 'View Withdrawn Children ',
                'href' => site_url('children/view_withdraw'),
                'subitem' => array('a' => 'a')
            )
        );
        
        $this->load->library('parser');
        $this->parser->parse('default/front_end/menu_left', $data);
        $this->template->parse_view('header', 'default/front_end/menu_left', $data);
    }

}

?>